using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using TMS.Application.Common.Interface;
using TMS.Application.RequestDto;
using TMS.Domain.Entity;

namespace TMS.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class TeachersList : ControllerBase
    {
        private readonly IRepository<Teachers> _Teachers;
        private readonly IRepository<TeaUsers> _TeaUsers;
        private readonly IRepository<Role> _Role;
        private readonly IRepository<Avatar> _Avatar;
        private readonly IRepository<Class> _Class;
        private readonly IRepository<ClassTeacher> _ClassTeacher;

        public TeachersList(IRepository<ClassTeacher> ClassTeacher, IRepository<Class> Class, IRepository<Avatar> Avatar, IRepository<Teachers> Teachers, IRepository<Role> Role, IRepository<TeaUsers> TeaUsers)
        {
            _Avatar = Avatar;
            _TeaUsers = TeaUsers;
            _Teachers = Teachers;
            _Role = Role;
            _Class = Class;
            _ClassTeacher = ClassTeacher;
        }
        //教师列表
        [HttpGet("/getTeachersList")]
        public object getTeachersList()
        {
            var Teachers = _Teachers.Table.OrderBy(time => time.CreatedAt).ToList();
            var Roles = _Role.Table.ToList();
            object[] TeaInfo = new object[Teachers.Count()];

            for (int i = 0; i < Teachers.Count(); i++)
            {
                var teaRoles = Roles.Where(x => x.TeacherId == Teachers[i].TeacherId).FirstOrDefault();
                if (teaRoles == null)
                {
                    // TeaInfo[i] = new
                    // {
                    //     Teachers[i].Id,
                    //     Teachers[i].TeacherId,
                    //     Teachers[i].TeacherName,
                    //     TeacherPhone = Teachers[i].PhoneNumber,
                    //     Teachers[i].ChattingNumber,
                    //     // IsMaster=false,

                    //     // IsInstructor=false,
                    //     // IsTeacher=false,
                    // };
                    continue;
                }
                else
                {
                    TeaInfo[i] = new
                    {
                        Teachers[i].Id,
                        Teachers[i].TeacherId,
                        Teachers[i].TeacherName,
                        TeacherPhone = Teachers[i].PhoneNumber,
                        Teachers[i].ChattingNumber,
                        teaRoles.IsMaster,

                        teaRoles.IsInstructor,
                        teaRoles.IsTeacher,
                    };
                }

            }

            return new
            {
                code = 200,
                data = TeaInfo,
                msg = "查询成功"
            };
        }
        [HttpPost("/addTeacher")]
        public async Task<object> addTeacher(TeacherInfoDto TeacherInfoDto)
        {
            var classes = _Class.Table.ToList();
            var teachers = _Teachers.Table.ToList();
            var TeaUsers = _TeaUsers.Table.ToList();//教师账号表
            var teaNum = teachers.Where(x => x.TeacherId == TeacherInfoDto.TeacherId).Count();
            if (teaNum > 0)
            {
                return new
                {
                    code = 400,
                    msg = "已有该教工",
                    data = ""
                };
            }
            else
            {
                var avatarId = _Avatar.Table.Where(x => x.CurrentName == "001.jpg").ToList();

                var tea = await _Teachers.AddAsync(new Teachers
                {
                    TeacherId = TeacherInfoDto.TeacherId,
                    TeacherName = TeacherInfoDto.TeacherName,
                    PhoneNumber = TeacherInfoDto.TeacherPhone,
                    ChattingNumber = TeacherInfoDto.ChattingNumber
                });
                var rol = await _Role.AddAsync(new Role
                {
                    TeacherId = tea.TeacherId,
                    IsInstructor = TeacherInfoDto.IsInstructor,
                    IsMaster = TeacherInfoDto.IsMaster,
                    IsTeacher = TeacherInfoDto.IsTeacher
                });
                var teaUser = await _TeaUsers.AddAsync(new TeaUsers
                {
                    UserId = TeacherInfoDto.TeacherId,
                    PassWord = "123456",
                    AvatarId = avatarId[0].Id,
                });
                ClassTeacher[] clas = new ClassTeacher[TeacherInfoDto.clasesId.Count];
                if (TeacherInfoDto.clasesId.Count > 0)
                {
                    for (int i = 0; i < TeacherInfoDto.clasesId.Count; i++)
                    {
                        clas[i] = new ClassTeacher
                        {
                            ClassId = TeacherInfoDto.clasesId[i],
                            TeacherId = tea.Id
                        };
                    }
                    await _ClassTeacher.AddBulkAsync(clas);

                }

                return new
                {
                    code = 200,
                    msg = "添加成功",
                    tea,
                    rol
                };
                //  return new { tea, rol };
            }


        }
        [HttpDelete("/delTeacher/{TeacherId}")]
        //删除教工
        public async Task<object> delTeacher(string teacherId)
        {
            var teachers = _Teachers.Table.ToList();
            var Roles = _Role.Table.ToList();
            var TeaUsers = _TeaUsers.Table.ToList();
            var teacher = teachers.Where(x => x.TeacherId == teacherId).ToList();

            if (teacher != null)
            {
                var Role = Roles.Where(x => x.TeacherId == teacher[0].TeacherId).ToList();
                var s = TeaUsers.FirstOrDefault(x => x.UserId == teacher[0].TeacherId);
                if (Role.Count != 0)
                {

                    await _Role.DeleteAsync(Role[0].Id, true);
                    await _Teachers.DeleteAsync(teacher[0].Id, true);
                    if (s != null)
                    {
                        await _TeaUsers.DeleteAsync(s.Id, true);
                        return new
                        {
                            code = 200,
                            msg = "删除成功",
                            data = ""
                        };

                    }
                    else
                    {
                        return new
                        {
                            code = 400,
                            msg = "删除失败",
                            data = ""

                        };
                    }


                }
                else
                {
                    return new
                    {
                        code = 400,
                        msg = "删除失败",
                        data = ""
                    };
                }

            }
            else
            {
                return new
                {
                    code = 400,
                    msg = "删除失败",
                    data = ""
                };
            }


            // return new{};
        }
        //EditTeacher编辑教工
        [HttpPost("/EditTeacher")]
        public async Task<object> EditTeacher(TeacherInfoDto TeacherInfoDto)
        {

            var Teachers = _Teachers.Table.ToList();
            var Roles = _Role.Table.ToList();
            var teacher = Teachers.Where(x => x.TeacherId == TeacherInfoDto.TeacherId).ToList();
            var Role = Roles.Where(x => x.TeacherId == TeacherInfoDto.TeacherId).ToList();
            if (teacher.Count() == 1)
            {
                if (Role.Count() == 1)
                {
                    Role[0].IsInstructor = TeacherInfoDto.IsInstructor;
                    Role[0].IsMaster = TeacherInfoDto.IsMaster;
                    Role[0].IsTeacher = TeacherInfoDto.IsTeacher;
                    await _Role.UpdateAsync(Role[0]);
                }
                teacher[0].TeacherName = TeacherInfoDto.TeacherName;
                teacher[0].PhoneNumber = TeacherInfoDto.TeacherPhone;
                teacher[0].ChattingNumber = TeacherInfoDto.ChattingNumber;
                await _Teachers.UpdateAsync(teacher[0]);
                return new
                {
                    code = 200,
                    msg = "编辑成功",
                    data = ""
                };
            }
            else
            {
                return new
                {
                    code = 200,
                    msg = "无该教工",
                    data = ""
                };
            }


        }
        //获得某一个要求的教师
        [HttpGet("/GetTeacher/{keyword}")]
        public async Task<object> GetTeacher(string keyword)
        {
            var teaS = _Teachers.Table.OrderBy(time => time.CreatedAt).ToList();
            var Roles = _Role.Table.ToList();
            var Teachers = teaS.Where(x => x.TeacherId == keyword || x.TeacherName == keyword).ToList();
            object[] TeaInfo = new object[Teachers.Count()];
            for (int i = 0; i < Teachers.Count(); i++)
            {
                var teaRoles = Roles.Where(x => x.TeacherId == Teachers[i].TeacherId).FirstOrDefault();
                if (teaRoles == null)
                {
                    continue;
                }
                else
                {
                    TeaInfo[i] = new
                    {
                        Teachers[i].Id,
                        Teachers[i].TeacherId,
                        Teachers[i].TeacherName,
                        TeacherPhone = Teachers[i].PhoneNumber,
                        Teachers[i].ChattingNumber,
                        teaRoles.IsMaster,

                        teaRoles.IsInstructor,
                        teaRoles.IsTeacher,
                    };
                }

            }
            return new
            {
                code = 200,
                data = TeaInfo,
                msg = "查询成功"
            };
        }
        [HttpGet("/GetClassId")]
        //获得所有班级id
        public Array GetClassId()
        {
            var classes = _Class.Table.ToList();
            Guid[] ClasesId = new Guid[classes.Count()];//所有班级id

            for (int i = 0; i < classes.Count(); i++)
            {
                ClasesId[i] = classes[i].Id;
            }
            return ClasesId;
        }

        //获得这个老师教的班级 id 和 班级名称
        [HttpGet("/GetTeaOfClass/{teacherId}")]
        public Array GetTeaOfClass(Guid teacherId)
        {
            var classTeachers = _ClassTeacher.Table.ToList();
            var classes = _Class.Table.ToList();
            var c = classTeachers.Where(x => x.TeacherId == teacherId).ToList();
            object[] classInfo = new object[c.Count()];
            if (c.Count() > 0)
            {
                for (int i = 0; i < c.Count(); i++)
                {
                    var classIn = classes.FirstOrDefault(x => x.Id == c[i].ClassId);
                    if (classIn != null)
                    {
                        classInfo[i] = new
                        {
                            classId = classIn.Id,
                            className = classIn.ClassName
                        };
                    }
                }

            }
            var tableHeadNameArr = (from str in classInfo where str != null select str).ToArray();

            return tableHeadNameArr;
        }
        //获得这老师没教的班级 id 和 班级名称
        //给教师单个添加班级//在InstructorController控制器中
        //给教师单个去除班级
        [HttpGet("/ClassAll/{teacherId}")]
        public object ClassAll(Guid teacherId)
        {
            var classes = _Class.Table.ToList();
            object[] obj = new object[classes.Count()];
            for (int i = 0; i < classes.Count(); i++)
            {
                obj[i] = new
                {
                    classId = classes[i].Id,
                    className = classes[i].ClassName
                };
            }
            object[] obj2 = (object[])GetTeaOfClass(teacherId);

            var chaji = obj.Except(obj2).ToList();
            return chaji;
        }

        [HttpPost("/delTC")]
        public async Task<object> delTC(delTCDto delTCDto)
        {
            var classTeachers = _ClassTeacher.Table.ToList();

            var s = classTeachers.FirstOrDefault(x => x.ClassId == delTCDto.ClassId && x.TeacherId == delTCDto.TeacherId);
            if (s != null)
            {
                await _ClassTeacher.DeleteAsync(s,true);
                return new
                {
                    code = 200,
                    msg = "删除成功",
                    dat = ""
                };

            }
            else
            {
                return new
                {
                    code = 400,
                    msg = "删除失败",
                    dat = ""
                };

            }
        }
    }
}